package com.lamou3g.code0509;

import org.apache.commons.io.IOUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * Created by dllo on 18/5/9.
 */
//ServletContext的自己的方法一:获取项目的路径
public class PathServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");//将文字设置成utf-8,防止浏览器乱码
        ServletContext app = getServletContext();
        //String path = app.getRealPath("Error404.html");
        PrintWriter writer = resp.getWriter();//要往网址上写东西,必须要写这句话(写的前提条件,调用这个写的方法)
        InputStream is=app.getResourceAsStream("Error404.html");//自动的获取项目的路径
        String html= IOUtils.toString(is);
        writer.println(html);
        is.close();
//        BufferedReader reader = new BufferedReader(new FileReader(path));
//        String line = "";
//        while ((line = reader.readLine()) != null) {
//            writer.println(line);
//        }
//        reader.close();
        writer.close();
    }
}
